package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.Func;
import com.company.project.entity.SysUser;
import com.company.project.entity.TSeatsEntity;
import com.company.project.mapper.TSeatsMapper;
import com.company.project.mapper.TZjhmglMapper;
import com.company.project.service.HttpSessionService;
import com.company.project.service.SysSmscodeService;
import com.company.project.service.UserService;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.TZjhmglEntity;
import com.company.project.service.TZjhmglService;



/**
 * 
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2023-11-09 11:24:45
 */
@Controller
@RequestMapping("/")
public class TZjhmglController {
    @Autowired
    private TZjhmglService tZjhmglService;
    @Autowired
    private TZjhmglMapper tZjhmglMapper;
    @Autowired
    private SysSmscodeService sysSmscodeService;
    @Autowired
    private HttpSessionService httpSessionService;
    @Autowired
    private UserService userService;
    @Autowired
    private TSeatsMapper tSeatsMapper;
    /**
    * 跳转到页面
    */
    @GetMapping("/index/tZjhmgl")
    public String tZjhmgl() {
        return "tzjhmgl/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("tZjhmgl/add")
    @RequiresPermissions("tZjhmgl:add")
    @ResponseBody
    public DataResult add(@RequestBody TZjhmglEntity tZjhmgl){
        //获取当前操作人
        //通过access_token拿userId
        String userId = httpSessionService.getCurrentUserId();
        SysUser user = userService.getById(userId);
        TSeatsEntity tSeatsEntity = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                .eq(TSeatsEntity::getAccountid, userId));
        tZjhmgl.setCompany(tSeatsEntity.getCorporatename());
        tZjhmgl.setUserid(user.getUsername());
        tZjhmgl.setZjr(userId);
        tZjhmgl.setAddtime(new Date());
        String start = tZjhmgl.getStart();
        String startNum = Func.equals("on",start)?"1": "2";
        tZjhmgl.setStart(startNum);
        tZjhmglService.save(tZjhmgl);

        //设置主叫
        if (Func.equals("on",start)){
            tZjhmglService.setCaller(tZjhmgl.getId());
        }
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("tZjhmgl/delete")
    @RequiresPermissions("tZjhmgl:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        tZjhmglService.removeByIds(ids);
        return DataResult.success();
    }
    @ApiOperation(value = "设置主叫")
    @PostMapping("tZjhmgl/setCaller")
    @LogAnnotation(title = "主叫", action = "设置主叫id")
    @ResponseBody
    public DataResult setCaller(@RequestBody @ApiParam(value = "id") String id){
        return DataResult.success( tZjhmglService.setCaller(id));
    }
    @ApiOperation(value = "设置主叫")
    @PostMapping("tZjhmgl/setCaller/zjhm")
    @LogAnnotation(title = "主叫", action = "设置主叫")
    @ResponseBody
    public DataResult setCallerByZjhm(@RequestBody  TZjhmglEntity tZjhmgl){
        String zjhm = tZjhmgl.getZjhm();
        String userId = httpSessionService.getCurrentUserId();
        SysUser sysUser = userService.getById(userId);
        TSeatsEntity tSeatsEntity = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery().eq(TSeatsEntity::getAccountid, userId));
        List<TZjhmglEntity> tZjhmglEntities = tZjhmglMapper.selectList(Wrappers.<TZjhmglEntity>lambdaQuery()
                .eq(TZjhmglEntity::getUserid, sysUser.getUsername()));
        tZjhmglMapper.updateZjhm(sysUser.getUsername());//全部设置未使用
        boolean flag = false;
        for (TZjhmglEntity tz: tZjhmglEntities) {
            if (Func.equals(zjhm,tz.getZjhm())) {
                tz.setStart("1");//设置为使用中
                flag = true;
                tZjhmglService.updateById(tz);
            }
        }
        if (!flag) {
            TZjhmglEntity tz = new TZjhmglEntity();
            tz.setStart("1");
            tz.setZjhm(zjhm);
            tz.setAddtime(new Date());
            tz.setCompany(tSeatsEntity.getCorporatename());
            tz.setUserid(sysUser.getUsername());
            tZjhmglService.save(tz);
        }

        return DataResult.success(zjhm);
    }
    @ApiOperation(value = "更新")
    @PutMapping("tZjhmgl/update")
    @RequiresPermissions("tZjhmgl:update")
    @ResponseBody
    public DataResult update(@RequestBody TZjhmglEntity tZjhmgl){
        tZjhmglService.updateById(tZjhmgl);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("tZjhmgl/checkYzm")
    @RequiresPermissions("tZjhmgl:update")
    @ResponseBody
    public DataResult checkYzm(@RequestBody TZjhmglEntity tZjhmgl){
        return tZjhmglService.checkYzm(tZjhmgl);
    }

    @ApiOperation(value = "发送验证码")
    @PutMapping("tZjhmgl/smdCheck/{id}")
    @ResponseBody
    public DataResult smdCheck(@PathVariable("id")  String id){
        TZjhmglEntity byId = tZjhmglService.getById(id);
        return sysSmscodeService.getSmscode(byId.getZjhm());
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("tZjhmgl/listByPage")
    @LogAnnotation(title = "主叫", action = "查询分页数据")
    @RequiresPermissions("tZjhmgl:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody TZjhmglEntity tZjhmgl){
        Page page = new Page(tZjhmgl.getPage(), tZjhmgl.getLimit());
        LambdaQueryWrapper<TZjhmglEntity> queryWrapper = Wrappers.lambdaQuery();
        //获取当前操作人
        //通过access_token拿userId
        String userId = httpSessionService.getCurrentUserId();
        SysUser sysUser = userService.getById(userId);
        //查询条件示例
        if (Func.isNotEmpty(tZjhmgl.getZjhm())){
            queryWrapper.eq(TZjhmglEntity::getZjhm, tZjhmgl.getZjhm());
        }
        if (Func.isNotEmpty(tZjhmgl.getUserid())){
            queryWrapper.eq(TZjhmglEntity::getUserid, tZjhmgl.getUserid());
        }
        //只能看自己名下的
        if (Func.isNotEmpty(sysUser)&&!Func.equals("1",userId)) {//管理员看全部
            queryWrapper.eq(TZjhmglEntity::getUserid,sysUser.getUsername());
        }
        queryWrapper.orderByDesc(TZjhmglEntity::getAddtime);
        IPage<TZjhmglEntity> iPage = tZjhmglService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
